<script>
/*
	This file is part of Limpet.

    Limpet is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Limpet is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Limpet.  If not, see http://www.gnu.org/licenses/.
	
	Copyright 2010 Dan Popick
*/

var x = localStorage.limpetSiteList;

var options = {
	siteList: x ? JSON.parse(localStorage.limpetSiteList) : []
};

console.log("background page startup.");

chrome.pageAction.onClicked.addListener(
	function(tab) {
		console.log("click for popup on url: " + tab.url);
	});

chrome.extension.onRequest.addListener(
	function(message, sender, sendResponse) {
		if (message) {
			console.log(JSON.stringify(message));
			switch(message.command) {
			    case "getSettingsByUrl":
					var site = getSiteByUrl(message.url);
					if (site) sendResponse(site);
					break;
				case "getSite":
					console.log("getting site for url: " + JSON.stringify(message.url));
					var site = getSiteByUrl({ href: message.url });
					console.log(JSON.stringify(site));
					sendResponse(site);
					break;
				case "pageLoaded":
					onPageLoaded(sender, message.url, message.data);
					break;
				case "update":
					options = message.options;
					break;
				case "openDisplay":
					openDisplay(message.site);
					break;
				default:
					console.log("Ignoring unknown message: " + message.command);
					break;
			}
		}
	});
	
function openDisplay(site) {
	var docLoc = chrome.extension.getURL("details.html");
	var tab;
	chrome.tabs.getAllInWindow(null, function(tabs) {
		var tab;
		for (var i = 0; i < tabs.length; ++i) {
			if (tabs[i].url == docLoc) {
				chrome.tabs.update(tabs[i].id, { selected: true });
			console.log("sending init to " + tabs[i].id.toString());
				chrome.tabs.sendRequest(tabs[i].id, {
					command: "init",
					"site": site
				});
				return;
			}
		}
		
		chrome.tabs.create({ url: docLoc }, function(tab) {
			console.log("sending init to " + tab.id.toString());
			chrome.tabs.sendRequest(tab.id, {
				command: "init",
				"site": site
			});
		});
	});
}	

function getPageDataByUrl(url) {
	var site = getSiteByUrl(url);
	return (site) ? site.data : null;
}

function onPageLoaded(sender, url, data)
{
	var site = getSiteByUrl(url);
	if (site) {
		site.data = JSON.stringify(data);
		var warnError = false;
		for (var i = 0; i < data.length; ++i) {
			var msg = data[i];
			if (msg.severity != "Debug" && msg.severity != "Info") {
				warnError = true;
			}
		}
		
		if (warnError)
			chrome.pageAction.setIcon({ tabId: sender.tab.id, path: "img/hotIcon.png" });
		else
			chrome.pageAction.setIcon({ tabId: sender.tab.id, path: "img/plainIcon.png" });
			
		chrome.pageAction.show(sender.tab.id);
		console.log(JSON.stringify(site));
	}
}

function getSiteByUrl(url)
{
	if (!url) return null;

	var i = 0;
	for (; i < options.siteList.length; ++i)
	{
		var site = options.siteList[i];
		if (!site.regex) site.regex = new RegExp(site.url, "i");

		if (site.regex.test(url.href)) return site;
	}
}
</script>